package cn.dyp.com.manualcontrol.adpter;


import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.dyp.com.manualcontrol.R;
import cn.dyp.com.manualcontrol.bean.ControllerUiData;

/**
 * 自定义控制器视图适配器
 * 
 * @author asus
 *
 */
public class ViewPagerAdapater extends PagerAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<ControllerUiData> mDatas;

	/**
	 * 监听接口
	 * 
	 * @author asus
	 *
	 */
	public interface OnItemOnClick {
		void click(int position);
	}

	private OnItemOnClick mItemOnClick;

	/**
	 * 构造函数
	 * 
	 * @param context
	 *            上下文对象
	 * @param mDatas
	 *            数据源
	 * @param itemOnClick
	 *            监听接口
	 */
	public ViewPagerAdapater(Context context, List<ControllerUiData> mDatas,
			OnItemOnClick itemOnClick) {
		this.mContext = context;
		this.mDatas = mDatas;
		this.mItemOnClick = itemOnClick;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 获取数据源长度
	 * 
	 * @see PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		return mDatas.size();
	}

	/**
	 * 判断两个对象是否相同
	 * 
	 * @see PagerAdapter#isViewFromObject(View,
	 *      Object)
	 */
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0.equals((View) arg1);
	}

	/**
	 * 销毁Item
	 * 
	 * @see PagerAdapter#destroyItem(ViewGroup,
	 *      int, Object)
	 */
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	/**
	 * 获取Item的位置
	 * 
	 * @see PagerAdapter#getItemPosition(Object)
	 */
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	/**
	 * 自定义视图
	 * 
	 * @see PagerAdapter#instantiateItem(ViewGroup,
	 *      int)
	 */
	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		final ControllerUiData data = mDatas.get(position);
		View view = mInflater.inflate(R.layout.controller_item, container,
				false);
		ImageView iconIV = (ImageView) view.findViewById(R.id.controller_icon);
		TextView nameTV = (TextView) view.findViewById(R.id.name_text_view);
		TextView statusTV = (TextView) view.findViewById(R.id.status_text_view);
		TextView openTV = (TextView) view.findViewById(R.id.open_text_view);

		if (data != null) {
			// 控制器名称
			nameTV.setText(data.name);
			String statusStr = mContext.getString(R.string.current_status);
			if (data.isOpen > 0) {
				// 设置控制器打开时的图标
				iconIV.setImageResource(data.onIcon);
				openTV.setText(R.string.close);
				statusStr += mContext.getString(R.string.open);

			} else {
				// 设置控制器关闭时的图标
				iconIV.setImageResource(data.offIcon);
				openTV.setText(R.string.open);
				statusStr += mContext.getString(R.string.close);

			}
			// 控制器状态
			statusTV.setText(statusStr);
			openTV.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mItemOnClick.click(position);
				}
			});
		}
		((ViewPager) container).addView(view);
		return view;
	}
}
